package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Together;
import cn.wolfcode.trip.base.domain.TogetherContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TogetherContentMapper;
import cn.wolfcode.trip.base.mapper.TogetherMapper;
import cn.wolfcode.trip.base.query.TogetherQueryObject;
import cn.wolfcode.trip.base.service.ITogetherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TogetherServiceImpl implements ITogetherService{
    //注入结伴游mapper
    @Autowired
    private TogetherMapper togetherMapper;
    //注入结伴游内容的mapper
    @Autowired
    private TogetherContentMapper contentMapper;

    /**
     * 结伴主页面的高级查询
     * @param qo
     * @return
     */
    @Override
    public PageInfo query(TogetherQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Together> list = togetherMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Together> selectAll() {
        return togetherMapper.selectAll();
    }


    @Override
    public Together selectTogetherById(Long id) {
        return togetherMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Together together) {
        //保存到结伴游表
        togetherMapper.insert(together);
        //获取结伴游内容对象
        TogetherContent content = together.getContent();
        //内容主键关联结伴游主键
        content.setId(together.getId());
        //保存到内容表
        contentMapper.insert(content);
    }

    @Override
    public List<User> getUserByTogetherId(Long togetherId) {
        return togetherMapper.selectUserByTogetherId(togetherId);
    }

    @Override
    public Boolean haveUser(Long togetherId,Long userId) {
        if(togetherMapper.selectUserByTogether(togetherId,userId)!=null){
            return true;
        }
        return false;
    }

    @Override
    public void deleteFocus(Long togetherId, Long userId) {
        togetherMapper.deleteFocus(togetherId,userId);
    }

    @Override
    public void saveFocus(Long togetherId, Long userId) {
        togetherMapper.insertRelation(togetherId,userId);
    }
}
